<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
   
    /**
     * 显示登录页面
     */

    public function getLogin()
    {
        return view('login.login');
    }

    //进行登录验证

    public function postCheck(Request $request)
    {
        //查询用户是否存在
        $user = DB::table('users')->where('username',$request->input('username'))->first();
        //检测密码是否一样
        if($user){
            if(Hash::check($request->input('password'),$user->password)){
                //登录成功 将用户id放入session中
                session(['id'=>$user->id,'name'=>$user->username]);
                return redirect('/admin')->with('success','欢迎登录');
            }else{
            return back()->with('error','用户名或密码错误');
            }
        }else{
            return back()->with('error','登录错误');
        }
    }

    //退出 清除session
    public function getLogout(Request $request)
    {
        $request->session()->flush();
        return redirect('/admin')->with('error','退出成功');
    }

}
